<?php
 /**
* Yii extension implementing NicEdit 0.9r23
*  NicEdit is an extremely lightweight WYSIWYG editor for websites
*  Coded by:  Brian Kirchoff
*  See : http://nicedit.com
*  Copyright: Brian Kirchoff
*
*  This widget is a wrapper for the above extentions
*
* @package NicEdit
* @author Loo
* @version 0.2
* @filesource NicEdit.php
*
* new nicEditor({buttonList : ['fontSize','bold','italic','underline','strikeThrough','subscript','superscript','html','image']}).panelInstance('area4');
*/
class NicEdit extends CWidget {

    public $id;
    public $options = array();
    private $_html;
    private $_baseUrl;

  /**
   * Execute the widget
   */
  public function run() {
    if(!isset ($this->id) || !$this->id)
      throw new CException("Can't create editor , 'id' is not set");
    $this->registerClientScripts();
    $this->createMarkup();
  }

  /**
   * Registers the clientside widget files (css & js and other files)
   */
  public function registerClientScripts() {
  // Get the resources path
    $resources = dirname(__FILE__).DIRECTORY_SEPARATOR.'resources';
    // publish the files
    $this->_baseUrl = Yii::app()->assetManager->publish($resources);
    Yii::app()->clientScript->registerScriptFile($this->_baseUrl.'/nicEdit.js');
  }

  /**
   * Creates the html markup needed by the widget
   */
    public function createMarkUp() {
        // Set NicEdit config
        $this->setDefaults();
        Yii::app()->clientScript->registerScript("NicEdit{$this->id}",
                "bkLib.onDomLoaded(function() {
                    new nicEditor({iconsPath :'$this->_baseUrl/{$this->options['iconsPath']}',buttonList : ['fontSize','bold','italic','underline','strikeThrough','ul','ol','subscript','superscript','upload','forecolor']}).panelInstance('$this->id');
	            });"
                , CClientScript::POS_HEAD);
        $this->_html="<textarea rows='{$this->options['rows']}' name='{$this->options['name']}' id='$this->id' style='width:{$this->options['width']};'></textarea>";
        echo $this->_html; // Final Html Output to view
    }


    /*
     * makeOptions sets default values to option not defined by user
     */
    private function setDefaults()
    {
        // Set defaults
        $this->setOption('defaultContent','');
        $this->setOption('name',$this->id);
        $this->setOption('rows',5);
        $this->setOption('iconsPath','nicEditorIcons.gif');
    }

    /*
     * setOption set default value if not defined already
     */
     private function setOption($optionKey,$defaultValue){
        if(!array_key_exists($optionKey, $this->options))
            $this->options[$optionKey] = $defaultValue;
     }

     /*
      * getOptions returns a string with the editor settings
      */
     /*private function getOptions()
     {
         $options = "iconsPath : '$this->_baseUrl/{$this->options['iconsPath']}'";

         return $options;
     }*/
}
?>
